//
//  JXPRecommendTableViewCell.m
//  模拟-百思不得姐
//
//  Created by ColinJ on 16/8/9.
//  Copyright © 2016年 colin. All rights reserved.
//

#import "JXPRecommendTableViewCell.h"
#import <MediaPlayer/MediaPlayer.h>
#import <AVKit/AVPlayerViewController.h>
#import "JXPMoviePlayer.h"


//单独音频
#define kMusicURL @"http://audio.xmcdn.com/group1/M01/2B/7C/wKgDrlLWMaSwIcbZAB76rhyUzA4434.mp3"
//单独视频
#define kVideoURL @"http://dd.yinyuetai.com/uploads/videos/common/9CB00154421D3966D6A52374737DEEDF.mp4?"

@interface JXPRecommendTableViewCell ()

@property(nonatomic,strong)  AVPlayer *player;
@property(nonatomic,strong)  UIImageView *Iview;
@property(nonatomic,strong) JXPRecommendModel *myModel;

@property (nonatomic,strong)MPMoviePlayerController *moviePlayer;


@end


@implementation JXPRecommendTableViewCell

//- (void)prepareForReuse {
//    
//    [super prepareForReuse];
//    [self.Iview reset];
////    [_voiceView reset];
//}

- (void)reset{
    
}

- (UIImageView *)Iview{
    if (!_Iview) {
        _Iview = [[UIImageView alloc]init];
    }
    return _Iview;
}

    
- (void)setupDataWithModel:(JXPRecommendModel *)model{
    self.myModel = model;
    [self.headView sd_setImageWithURL:[NSURL URLWithString:model.profile_image] placeholderImage:[UIImage imageNamed:@"AppIcon29x29~ipad"]];
    self.timeLabel.text = model.create_time;
    self.desTextLabel.text = model.text;
    self.nickNameLabel.text = model.name;
    self.myTagLabel.text = model.tag;
    [self.caiBtn setTitle:model.cai forState:(UIControlStateNormal)];
    [self.zanBtn setTitle:model.love forState:UIControlStateNormal];
    [self.shareBtn setTitle:model.repost forState:(UIControlStateNormal)];
    [self.recommandBtn setTitle:model.comment forState:(UIControlStateNormal)];
    self.myChefView.clipsToBounds = YES;
    int n = (int)model.type;
    switch (n) {
//帖子的类型，1为全部 10为图片 29为段子 31为音频 41为视频
        case 10:{
            if ([model.is_gif intValue] == 1) {
                UIImageView *gifView = [[UIImageView alloc]initWithFrame:self.myChefView.bounds];
                gifView.userInteractionEnabled = NO;
                [gifView sd_setImageWithURL:[NSURL URLWithString:model.image1]];
                [self.myChefView addSubview:gifView];
//                self.Iview.frame = self.myChefView.bounds;
//                _Iview.userInteractionEnabled = NO;
//                [_Iview sd_setImageWithURL:[NSURL URLWithString:model.image1] placeholderImage:[UIImage imageNamed:@"empty_failed"]];
//                [self.myChefView addSubview:_Iview];
            }else{
                UIImageView *imgView = [[UIImageView alloc]initWithFrame:self.myChefView.bounds];
                imgView.contentMode = UIViewContentModeCenter;
                [imgView sd_setImageWithURL:[NSURL URLWithString:model.cdn_img]];
                imgView.userInteractionEnabled = YES;
                self.Iview.frame = CGRectMake(0, 0, _myChefView.size.width, _myChefView.size.height);
                UIGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tapAction:)];
                [imgView addGestureRecognizer:tap];
                [self.myChefView addSubview:imgView];

//                _Iview.contentMode = UIViewContentModeCenter;
//                _Iview.clipsToBounds = YES;
//                [_Iview sd_setImageWithURL:[NSURL URLWithString:model.cdn_img] placeholderImage:[UIImage imageNamed:@"empty_failed"]];
//                UIGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tapAction:)];
//                [_Iview addGestureRecognizer:tap];
//                _Iview.userInteractionEnabled = YES;
//                [self.myChefView addSubview:_Iview];
            }
                    }
            break;
        case 41:{
            self.Iview.frame = CGRectMake(0, 0, _myChefView.size.width, _myChefView.size.height);
            _Iview.contentMode = UIViewContentModeCenter;
            _Iview.clipsToBounds = YES;
            _Iview.userInteractionEnabled = NO;
            [_Iview sd_setImageWithURL:[NSURL URLWithString:model.cdn_img]];
            UIButton *playBtn = [UIButton buttonWithType:(UIButtonTypeCustom)];
            playBtn.frame = CGRectMake(0, 0, 50, 50);
            playBtn.center = _Iview.center;
            [playBtn setImage:[UIImage imageNamed:@"video-play"] forState:(UIControlStateNormal)];
            [playBtn addTarget:self action:@selector(playClick) forControlEvents:(UIControlEventTouchUpInside)];
            [self.myChefView addSubview:_Iview];
            [self.myChefView insertSubview:playBtn aboveSubview:_Iview];
        }
            break;
        default:
            break;
    }
}

- (void)playClick{
    for (UIView *subView in self.myChefView.subviews) {
        [subView removeFromSuperview];
    }
    [JXPMoviePlayer shareMoviePlayer].playerView = self.myChefView;
    [[JXPMoviePlayer shareMoviePlayer]playViewWithURLString:self.myModel.videouri];
}



- (void)tapAction:(UITapGestureRecognizer *)tap{
    self._pushBlock(self.myModel);
}


- (void)playView{
    AVPlayerItem *item = [[AVPlayerItem alloc]initWithURL:[NSURL URLWithString:kVideoURL]];
    self.player = [[AVPlayer alloc]init];
    [self.player replaceCurrentItemWithPlayerItem:item];
    
    
    AVPlayerLayer *playerLayer = [AVPlayerLayer playerLayerWithPlayer:self.player];
    playerLayer.frame = self.myChefView.bounds;
    playerLayer.videoGravity=AVLayerVideoGravityResizeAspectFill;  //视频填充模式
    [_myChefView.layer addSublayer:playerLayer];

}

- (void)moviePlay{
    AVPlayerViewController *playerViewController = [[AVPlayerViewController alloc] init];
    [self.myChefView addSubview:playerViewController.view];
    [playerViewController isReadyForDisplay];
//    [playerViewController.player play];
    _player = playerViewController.player;
    AVPlayerItem *item = [[AVPlayerItem alloc]initWithURL:[NSURL URLWithString:_myModel.videouri]];
    [_player replaceCurrentItemWithPlayerItem:item];
}

- (void)moviePlayertest{
    NSURL *url = [NSURL URLWithString:self.myModel.videouri];
    self.moviePlayer = [[MPMoviePlayerController alloc] initWithContentURL:url];
    _moviePlayer.view.frame = CGRectMake(0, 0, self.myChefView.frame.size.width, self.myChefView.frame.size.height);
    [self.myChefView addSubview:_moviePlayer.view];
    _moviePlayer.shouldAutoplay = YES;
    UIButton *playBtn = [UIButton buttonWithType:(UIButtonTypeCustom)];
    [playBtn setImage:[UIImage imageNamed:@"playButtonPause"] forState:(UIControlStateNormal)];
    playBtn.frame = CGRectMake(0, 0, 50, 50);
    playBtn.center = _moviePlayer.view.center;
    [playBtn addTarget:self action:@selector(playAndPause) forControlEvents:(UIControlEventTouchUpInside)];
    [_moviePlayer.view addSubview:playBtn];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(finishPlay) name:MPMoviePlayerPlaybackDidFinishNotification object:nil];
}

- (void)playAndPause{
    [_moviePlayer play];
}

- (void)finishPlay{
     [self.moviePlayer stop];
}



- (void)awakeFromNib {
    [super awakeFromNib];
    self.selectionStyle = UITableViewCellSelectionStyleNone;
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
